import axios from 'axios';
import Qs from 'qs';
import {
  getStore,
  setStore
} from './storage';
import {
  router
} from '../router/index';
import {
  Message
} from 'view-design';
import Cookies from 'js-cookie';
import config from '@/config'

// 统一请求路径前缀
// let base = '/xboot';
const base = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro

export const BASE_URL = base
// 超时设定
axios.defaults.timeout = 15000;

axios.interceptors.request.use(config => {
  return config;
}, err => {
  Message.error('请求超时');
  return Promise.resolve(err);
});

// 请求拦截
axios.interceptors.request.use(config => {
  if (!config.url.includes('/login')) {
    config.headers['token'] = getStore('token');
  }
  return config
}, error => {
  return Promise.reject(error)
})

// http response 拦截器
axios.interceptors.response.use(response => {
  const data = response.data;
  return data;
}, (err) => {
  // 根据返回的code值来做不同的处理(和后端约定)
  if (err && err.response) {
    switch (err.response.status) {
      case 401:
        Message.error("请重新登录！");
        // 未登录 清除已登录状态
        Cookies.set('userInfo', '');
        setStore('token', '');
        router.replace('/login');
        break;
      case 403:
        // 没有权限
        Message.error("请重新登录！");
        Cookies.set('userInfo', '');
        setStore('token', '');
        router.replace('/login');
        break;
      case 500:
        // 错误
        if (err.response.data) {
          Message.error(err.response.data);
        } else {
          Message.error("未知错误");
        }
        break;
      default:
        Message.error(err.toString());
        return data;
    }
  }
  return Promise.resolve(err);
});

export const getRequest = (url, params) => {
  return axios({
    method: 'get',
    url: `${base}${url}`,
    params: params,
    // headers: {
    //     'token': token
    // }
  });
};

export const postRequest = (url, params) => {
  return axios({
    method: 'post',
    url: `${base}${url}`,
    data: params,
    // transformRequest: [function (data) {
    //     let ret = '';
    //     for (let it in data) {
    //         ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
    //     }
    //     return ret;
    // }],
    headers: {
      'Content-Type': 'application/json',
      // 'token': token
    }
  });
};

export const formRequest = (url, params) => {
  return axios({
    method: 'post',
    url: `${base}${url}`,
    data: params,
    transformRequest: [function(data) {
      return Qs.stringify(data);
    }],
    headers: {
      "Content-Type": 'application/x-www-form-urlencoded; charset=UTF-8'
      //'token': token
    }
  });
};

export const putRequest = (url, params) => {
  return axios({
    method: 'put',
    url: `${base}${url}`,
    data: params,
    transformRequest: [function(data) {
      let ret = '';
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
      }
      return ret;
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    }
  });
};

export const deleteRequest = (url, params) => {
  return axios({
    method: 'delete',
    url: `${base}${url}`,
    params: params,

  });
};

export const uploadFileRequest = (url, params) => {
  return axios({
    method: 'post',
    url: `${base}${url}`,
    params: params,

  });
};
